import { defineStore } from 'pinia'
// 导入axios
import request from '@/utils/request'
import { IApiRes, GoodsInfo } from '@/types/data'
// 应为只有汉字，所以给它添加一个id属性
export default defineStore('goods', {
    state: () => ({
        // 商品详细信息
        info: {} as GoodsInfo
    }),
    actions: {
        // 获取商品详情信息
        async getGoodsInfo(id: string) {
            const { data: { result } } = await request.get<IApiRes<GoodsInfo>>('/goods', {
                params: {
                    id
                }
            })
            this.info = result
        },

        // 查询当前商品价格
        updatedPrice(id: string) {
            const item = this.info.skus.find(it => it.id === id)
            // 表示当前用户选择的sky对应的商品信息
            if (item) {
                this.info.price = item.price
                this.info.oldPrice = item.oldPrice
                this.info.inventory = item.inventory
            }
        },
    },
    getters: {},

    // 数据持久化插件设置
    persist: {
        enabled: true,
        strategies: [
            { key: 'goods' }
        ]
    }
})